package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.common.ResultType;
import com.engineering.elink.domain.Person;
import com.engineering.elink.service.PersonService;
import com.engineering.elink.service.SignService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/sign")
@Api(value = "签到接口")
public class SignController {
	@Autowired
	private SignService signService;
	@Autowired
	private PersonService personService;
	
	/**
	 * 点击签到
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "signUp", method = RequestMethod.GET)
	@ApiOperation(value = "签到")
	public Map<String, Object> signUp(HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			if (signService.signUp(userId)) {
				return MapConvert.getMap(ResultType.SUCCESS);
			}else {
				return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
			}
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	

	/**
	 * 获取签到信息
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "getSignInfo", method = RequestMethod.GET)
	@ApiOperation(value = "获取签到信息")
	public Map<String, Object> getSignInfo(HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			Map<String,Object> map = new HashMap<String,Object>();
			Person person = personService.getPersonAllInfo(userId);
			map.put("isSign", signService.checkSign(userId));
			map.put("maxSignCount", person.getMaxSignCount());
			map.put("succesiveSignCount", person.getSuccesiveSignCount());
			return MapConvert.getMap(ResultType.SUCCESS, map);
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
}
